<?
//--模板le.je--
class richmodule {
	
   public $listmat=array();
   public $m=0; //统计内嵌循环
   public $root="/var/www/html/new/";
	
   public function Fetch_Module_File($file) {
	  ob_start();
	  require_once($file);
      $this->html=ob_get_contents();
      ob_end_clean(); 
  
   }
   
   public function Fetch_Array($str, $arr, $n=0, $m=0) {
	   if ($n==0) $arr=array($arr);
	   
	   if (!is_array($arr[0][0])) {
			echo "Error:Not Really Array!";
			exit;
			return;
	    }  
	   if ($str) {
		 foreach ($arr as $kk=>$vv) {
		   //$a_arg=explode(',',$arg);
		   foreach ($vv[0] as $key=>$val) {
			   $s1=$str.$key.$kk;
			   $a1=array();
			   foreach ($vv as $v) {
				   $a1[]=$v[$key];
			    
			   }
			   $this->$s1=$a1;
		   }
		    $k1="c".$str.$kk;
            if ($m==0) $this->$k1=count($vv);
			else $this->$k1=0;   
	    }
		$k2="c".$str;
        if ($m==0) $this->$k2=count($vv);
		else $this->$k2=0; 
	   }

   }
   
   public function Fetch_String($str, $s) {
	  //$this->$str=$s;
	  $this->html=str_replace("{\$".$str."}", $s, $this->html);
   }   
   
   /*public function Set_On_Array(){

	  if ($this->ci>0) {
		 foreach ($this->listmat[0] as $k=>$v) {	
	     $txtv=str_replace(array("<!--&","-->"),"",$v);
	     parse_str($txtv); 
		 echo "<br>txtv:".$txtv."<br>";
		 $a_arg=explode(',',$arg);
	     foreach ($a_arg as $key=>$val){
		    $pat=array();
		    for ($i=1; $i<=$limit; $i++) {
		     $pat[]='/\{'.$list.'\.'.$val.$i.'\}/';
		    }
			$c=$list.$val;
	        $this->html=preg_replace($pat,$this->$c,$this->html);	
	      }		 
		 }
	  }
   }*/
   
   public function Set_Array_On(){
	  $ci=preg_match_all('/<!--&list.+?-->/',$this->html,$this->listmat);
	  //print_r($this->listmat);
	  if ($ci>0){
	  foreach ($this->listmat[0] as $v){
	   //$vt=str_replace(array('<','>'),array('\<','\>'),$v);
	   //echo $vt='/'.$vt.'.+?\<!--list--\>/';
	   //$ti=preg_match_all('/<!--&([\w\W]*?)-->/',$this->html,$htmlmat);
	   $txtv=str_replace(array("<!--&","-->"),"",$v);
	   parse_str($txtv);
	   $str_pat='/'.$v.'([\w\W]*?)<!--list-'.$list.'-->/';
	   
	   do {
	   $htmlmat=array();
	   $ti=preg_match_all($str_pat,$this->html,$htmlmat);
	   
	   if ($ti<0) {
			echo "Error:Wrong List Number!".$ti;
			exit;
			return;		   
	   }
	   //print_r($htmlmat);
	   //取得数组总数
	   $a1="c".$list;
	   if ($child==0) $this->m=0;
	   $a1.=$this->m;
	   
	   if ($child==1 && $limit==0) $limit1=$this->$a1?$this->$a1:0;
	   else $limit1=$limit?$limit:$this->$a1;
	   
	   //分解参数
	   $a_arg=explode(',',$arg);
	   
	   //取得循环替代内容
	   $rhtml=$htmlmat[0][0];
	   $rehtml=$htmlmat[1][0];
	   
	   
	   
	      $retxt="";
	      for ($i=0; $i<$limit1; $i++) {
			  $pat=array();
			  $aab=array();
			 foreach ($a_arg as $val) {
		     $pat[]='/\{'.$list.'\.'.$val.'\}/';
			 //取得数组
			 $ac=$list.$val.$this->m;
			 $aac=$this->$ac;
			 $aab[]=$aac[$i];
			 //echo "pat$i:".$pat."\n";
			 //echo "aac$i:".$aac[$i]."\n";
			 //echo $rehtml;
			 }
			 $retxt.=preg_replace($pat,$aab,$rehtml,1);
		  }
	   
	   //取代循环代码
	  //echo $rhtml;
	   $this->html=$this->str_replace_once($rhtml, $retxt, $this->html);
	   if ($child==1) $this->m++;

	   }while($child==1 && $ti>=1);
	   
	  }
	  }
   }
   
   public function Output_Html(){
	 echo $this->html;	   
   }

   public function Make_If_Module($title, $s) {
	  $a_if=array();
	  //echo $this->html;
	  $c_if=preg_match_all('/<!--&if='.$title.'.+?-->/',$this->html,$a_if);
	  foreach ($a_if[0] as $v) {
	  $a_if_str=str_replace(array("<!--&","-->"),"",$v);
	  parse_str($a_if_str);
	  $str_pat='/'.$v.'([\w\W]*?)<!--if-'.$title.'-'.$status.'-->/';
	  $ak=array();
	  preg_match_all($str_pat,$this->html,$ak);
	  $rak0=$ak[0][0];
	  $rak1=$ak[1][0];
	  if ($status==$s)  $this->html=$this->str_replace_once($rak0, $rak1, $this->html);
	  else $this->html=$this->str_replace_once($rak0, "", $this->html);
	  }
   }

   public function Make_Html($url){
	 $fp=fopen($this->root.$url,"w");
     fputs($fp,$this->html);
     fclose($fp);	   
   }
   
   public function Include_File($url) {
	  $this->html=str_replace("{-".$url."-}", "<? include('".$this->root.$url."');?>", $this->html);  
   }
   
   public function str_replace_once($needle, $replace, $haystack) {
     $pos = strpos($haystack, $needle);
     if ($pos === false) {
     return $haystack;
     }
     return substr_replace($haystack, $replace, $pos, strlen($needle));
    }
   
}//class结束

?>